import os, geopandas as gpd
#primero utilizaremos la base del mapa -> sus fronteras
ucrania=gpd.read_file(os.path.join("maps","Fronteras_Ucrania","Fronteras_Ucrania.shp"))
#qué tipo de archivo es?
type(ucrania)
geopandas.geodataframe.GeoDataFrame
#Vamos a observar la dimensión del archivo
ucrania.shape
(2, 2)
#veamos el contenido de esta variable
ucrania.head
<bound method NDFrame.head of Tipo geometry 0 Marítimas MULTILINESTRING ((277198.575 5014665.224, 2771... 1 Terrestres LINESTRING (241428.870 5012615.860, 241414.318...>
#el archivo tiene algún valor vacío?
ucrania[ucrania.isna().any(axis=1)]
| Tipo | geometry |
|---|
#No tiene ningún valor vacío
#Qué tipo de variables tiene
ucrania.info()
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 2 entries, 0 to 1 Data columns (total 2 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Tipo 2 non-null object 1 geometry 2 non-null geometry dtypes: geometry(1), object(1) memory usage: 160.0+ bytes
#podemos observar que tenemos una columna con Dtype Geometry,
#por lo que podemos hacer lo siguiente
ucrania.plot()
<Axes: >
#ahora también observaremos los otros shapefiles
ciudades=gpd.read_file(os.path.join("maps","Ciudades_Ucrania","Ciudades_Ucrania.shp"))
regiones=gpd.read_file(os.path.join("maps","Salario_Medio_2013_Ucrania","Salario_Medio_2013_Ucrania.shp"))
capitales=gpd.read_file(os.path.join("maps","Capitales_Oblasts_Ucrania","Capitales_Oblasts_Ucrania.shp"))
hidrografia=gpd.read_file(os.path.join("maps","Hidrografia_Ucrania","Hidrografia_Ucrania.shp"))
ciudades.head()
| OBJECTID_1 | Español | UcrRomaniz | Ucraniano | Ruso | Tipo | geometry | |
|---|---|---|---|---|---|---|---|
| 0 | 1 | Sebastopol | Sevastopolska | Севастопольська | Севастопольский | Capital de Oblast | POINT (552916.756 4931924.758) |
| 1 | 2 | Yaltynska | Yaltynska | Ялтинська | Ялтинский | Ciudad | POINT (593790.870 4931951.790) |
| 2 | 3 | Nakhimovskyi | Nakhimovskyi | Нахімовський | Нахимовский | Ciudad | POINT (551941.685 4949563.926) |
| 3 | 4 | Bakhchysaraiskyi | Bakhchysaraiskyi | Бахчисарайський | Бахчисарайский | Ciudad | POINT (572639.833 4950149.568) |
| 4 | 5 | Alushtynska | Alushtynska | Алуштинська | Алуштинский | Ciudad | POINT (615082.784 4957952.483) |
regiones.head()
| OBJECTID | Español | UcrRomaniz | Ucraniano | Ruso | UAH | Hryvnia | geometry | |
|---|---|---|---|---|---|---|---|---|
| 0 | 1 | República Autónoma de Crimea | Avtonomna Respublika Krym | Автономна Республіка Крим | Автономная Республика Крым | 2600 | 2600 | MULTIPOLYGON (((686411.045 5014674.659, 686340... |
| 1 | 2 | Ciudad de Sebastopol | Sevastopol | Севастопольська | Севастополь | 2900 | 2900 | POLYGON ((544462.430 4965326.072, 544506.660 4... |
| 2 | 3 | Ciudad de Kiev | Kyivska | Київська | Киевская | 3900 | 3900 | POLYGON ((338757.140 5606777.580, 338758.542 5... |
| 3 | 4 | Cherníhiv | Chernihivska | Чернігівська | Черниговская | 2300 | 2300 | POLYGON ((513629.082 5802615.130, 514017.226 5... |
| 4 | 5 | Chernivtsí | Chernivetska | Чернівецька | Черновицкая | 2300 | 2300 | POLYGON ((-27501.160 5411499.428, -27479.092 5... |
capitales.head()
| OBJECTID_1 | Español | UcrRomaniz | Ucraniano | Ruso | Tipo | geometry | |
|---|---|---|---|---|---|---|---|
| 0 | 1 | Sebastopol | Sevastopolska | Севастопольська | Севастопольский | Capital de Oblast | POINT (552916.756 4931924.758) |
| 1 | 8 | Simferopolskyi | Simferopolskyi | Сімферопольський | Симферопольский | Capital de Oblast | POINT (564534.544 4983019.633) |
| 2 | 47 | Odesa | Odeska | Одеська | Одесский | Capital de Oblast | POINT (324536.092 5149464.783) |
| 3 | 57 | Jersón | Khersonska | Херсонська | Херсонский | Capital de Oblast | POINT (470981.515 5169674.476) |
| 4 | 72 | Mykoláiv | Mykolaivska | Миколаївська | Николаевский | Capital de Oblast | POINT (424909.281 5199964.550) |
hidrografia.head()
| NOMBRE | geometry | |
|---|---|---|
| 0 | San | MULTILINESTRING ((-238915.129 5477644.045, -23... |
| 1 | Dniester | MULTILINESTRING ((-235855.759 5496822.186, -23... |
| 2 | Prut | MULTILINESTRING ((-127633.636 5362417.577, -12... |
| 3 | Siret | LINESTRING (-18363.591 5337838.709, -18388.160... |
| 4 | Danubio | MULTILINESTRING ((126427.190 5046636.385, 1274... |
#Tenemos puntos, líneas y polígonos
#Vamos a identificar si cada uno de estos posee valores faltantes
ciudades[ciudades.isna().any(axis=1)]
| OBJECTID_1 | Español | UcrRomaniz | Ucraniano | Ruso | Tipo | geometry |
|---|
regiones[regiones.isna().any(axis=1)]
| OBJECTID | Español | UcrRomaniz | Ucraniano | Ruso | UAH | Hryvnia | geometry |
|---|
capitales[capitales.isna().any(axis=1)]
| OBJECTID_1 | Español | UcrRomaniz | Ucraniano | Ruso | Tipo | geometry |
|---|
hidrografia[hidrografia.isna().any(axis=1)]
| NOMBRE | geometry |
|---|
#ninguno posee un valor faltante
#usaremos nuevamente .plot() para ver los gráficos
ciudades.plot()
<Axes: >
regiones.plot()
<Axes: >
capitales.plot()
<Axes: >
hidrografia.plot()
<Axes: >
#Ahora realizaremos unos ajustes visuales capa por capa para ver sus resultados
#En general, no le pondremos ningún color al siguiente polígono, mas sí le agregamos uno al contorno para diferenciarlo
#También se cambia el grosor de este
ucrania.plot(edgecolor="red",
linewidth=1.2)
<Axes: >
#A las regiones le pondremos un color "Cornsilk" y su borde será negro
#También le disminuiremos el grosor de sus contornos
regiones.plot(facecolor="cornsilk",
edgecolor="black",
linewidth=0.5)
<Axes: >
#Son muchas ciudades, les pondremos el color "greenyellow"; además,
#Tendrán un borde de color negro
#Las añadiremos con mayor transparencia y su tamaño será menor que el de las capitales
ciudades.plot(facecolor="greenyellow",
edgecolor="black",
markersize=5,
alpha=0.6)
<Axes: >
#Mostraremos las capitales como una estrella
#Les pondremos un color que resalte y tendrán un tamaño mayor
capitales.plot(facecolor="fuchsia",
marker="*",
markersize=40)
<Axes: >
#Por último, añadimos la hidrografía
#Le cambiamos de color, el estilo de las líneas y su grosor
hidrografia.plot(edgecolor="dodgerblue",
linestyle="dashdot",
linewidth=1.5)
<Axes: >
#Ahora verificaremos que todos tengan la misma proyección, para que las capas concuerden con el mapa
ucrania.crs
<Projected CRS: EPSG:25836> Name: ETRS89 / UTM zone 36N Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: Europe between 30°E and 36°E: Finland - onshore and offshore; Norway including Svalbard - onshore and offshore. - bounds: (30.0, 61.73, 36.01, 84.7) Coordinate Operation: - name: UTM zone 36N - method: Transverse Mercator Datum: European Terrestrial Reference System 1989 ensemble - Ellipsoid: GRS 1980 - Prime Meridian: Greenwich
ciudades.crs
<Projected CRS: EPSG:25836> Name: ETRS89 / UTM zone 36N Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: Europe between 30°E and 36°E: Finland - onshore and offshore; Norway including Svalbard - onshore and offshore. - bounds: (30.0, 61.73, 36.01, 84.7) Coordinate Operation: - name: UTM zone 36N - method: Transverse Mercator Datum: European Terrestrial Reference System 1989 ensemble - Ellipsoid: GRS 1980 - Prime Meridian: Greenwich
regiones.crs
<Projected CRS: EPSG:25836> Name: ETRS89 / UTM zone 36N Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: Europe between 30°E and 36°E: Finland - onshore and offshore; Norway including Svalbard - onshore and offshore. - bounds: (30.0, 61.73, 36.01, 84.7) Coordinate Operation: - name: UTM zone 36N - method: Transverse Mercator Datum: European Terrestrial Reference System 1989 ensemble - Ellipsoid: GRS 1980 - Prime Meridian: Greenwich
capitales.crs
<Projected CRS: EPSG:25836> Name: ETRS89 / UTM zone 36N Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: Europe between 30°E and 36°E: Finland - onshore and offshore; Norway including Svalbard - onshore and offshore. - bounds: (30.0, 61.73, 36.01, 84.7) Coordinate Operation: - name: UTM zone 36N - method: Transverse Mercator Datum: European Terrestrial Reference System 1989 ensemble - Ellipsoid: GRS 1980 - Prime Meridian: Greenwich
hidrografia.crs
<Projected CRS: EPSG:25836> Name: ETRS89 / UTM zone 36N Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: Europe between 30°E and 36°E: Finland - onshore and offshore; Norway including Svalbard - onshore and offshore. - bounds: (30.0, 61.73, 36.01, 84.7) Coordinate Operation: - name: UTM zone 36N - method: Transverse Mercator Datum: European Terrestrial Reference System 1989 ensemble - Ellipsoid: GRS 1980 - Prime Meridian: Greenwich
#Como se observa, todos poseen la misma proyección (EPSG:25836); por lo tanto,
#no es necesario utilizar to.crs() en las capas
#Es hora de construir, pondremos la base
#Esta será considerada como el contorno del país
#También la cambiaremos de tamaño
base=ucrania.plot(edgecolor="red",linewidth=1.2, figsize=(12,12))
#Ahora que está la base, añadiremos las capas
#ax=base para que se pongan sobre esta
regiones.plot(facecolor="cornsilk", edgecolor="black", linewidth=0.5, ax=base)
hidrografia.plot(edgecolor="dodgerblue", linestyle="dashdot", linewidth=1.5, ax=base)
ciudades.plot(facecolor="greenyellow", edgecolor="black", markersize=5, alpha=0.6, ax=base)
capitales.plot(facecolor="fuchsia", marker="*", markersize=40, ax=base)
<Axes: >
#Ahora lo presentaremos de manera interactiva
#Importaremos folium
import folium
#Las ciudades serán mostradas de color rojo
#Las capitales serán mostradas de color negro (algunas ciudades son capitales)
#La hidrografía será mostrada de color azul
#La fronterá será de color oro
m=regiones.explore(color='gray', name="salario")
m=ucrania.explore(m=m, color="gold",name="borde")
m=ciudades.explore(m=m,color="red", name="ciudades")
m=capitales.explore(m=m, color="black", name="capitales")
m=hidrografia.explore(m=m, color="dodgerblue", name="hidrografia")
folium.LayerControl().add_to(m)
m